package br.com.virtual.base.controller.vender;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.primefaces.event.ToggleEvent;

import br.com.virtual.base.controller.util.VirtualMB;
import br.com.virtual.base.enums.StatusVendaEnum;
import br.com.virtual.base.exceptions.VirtualValidationException;
import br.com.virtual.base.service.SenderEmail;
import br.com.virtual.vender.model.Venda;
import br.com.virtual.vender.service.VendaService;

@ManagedBean
@ViewScoped
public class VendasRealizadasMB extends VirtualMB {

	private static final long serialVersionUID = 1L;
	private StatusVendaEnum statusVendasExibir;
	private List<Venda> vendas;
	private List<StatusVendaEnum> status;
	private StatusVendaEnum novoStatus;
	private Venda venda;

	@EJB private SenderEmail senderEmail;
	@EJB private VendaService vendaService;

	
	@PostConstruct
	private void init() {
		this.statusVendasExibir = (StatusVendaEnum) this.getFlash("statusExibir");
		this.statusVendasExibir = StatusVendaEnum.EM_TRANSPORTE;
		this.getUsuarioLogado();
	}

	private List<Venda> carregarVendas() {

		if (this.hasUsuarioAutenticado()) {

			if (this.statusVendasExibir.equals(StatusVendaEnum.EM_TRANSPORTE))
				return this.vendaService.getListEmAndamento();
			else
				return this.vendaService.getList();

		} else
			return new ArrayList<Venda>();
	}

	public void alterarStatus(Venda venda) {

		try {

			if (this.novoStatus.ordinal() > this.venda.getStatus().ordinal()) {
				this.venda.setStatus(this.novoStatus);
				this.vendaService.update(venda);
				
				this.senderEmail.sendVendaStatusAlterado(venda.getCliente().getEmail(), venda.getCliente().getNome(), 
						venda.getId().toString(), venda.getStatus());
			}

		} catch (VirtualValidationException e) {
			e.printStackTrace();
		}
	}

	public void onRowToggle(ToggleEvent event) {

		this.venda = (Venda) event.getData();
		this.venda.setItens(this.vendaService.getItens(this.venda));
		this.novoStatus = this.venda.getStatus();

		this.status = new ArrayList<StatusVendaEnum>();
		for (StatusVendaEnum s : StatusVendaEnum.values())
			this.status.add(s);

	}

	/*
	 * getters and setters
	 */

	public List<Venda> getVendas() {

		if (vendas == null)
			this.vendas = carregarVendas();

		return vendas;
	}

	public void setVendas(List<Venda> vendas) {
		this.vendas = vendas;
	}

	public StatusVendaEnum getStatusVendasExibir() {
		return statusVendasExibir;
	}

	public void setStatusVendasExibir(StatusVendaEnum statusVendasExibir) {
		this.statusVendasExibir = statusVendasExibir;
	}

	public Venda getVenda() {
		return venda;
	}

	public void setVenda(Venda venda) {
		this.venda = venda;
	}

	public List<StatusVendaEnum> getStatus() {
		return status;
	}

	public void setStatus(List<StatusVendaEnum> status) {
		this.status = status;
	}

	public StatusVendaEnum getNovoStatus() {
		return novoStatus;
	}

	public void setNovoStatus(StatusVendaEnum novoStatus) {
		this.novoStatus = novoStatus;
	}
}
